package com.hsd.app.action;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.hsd.app.model.HsdEffctRelation;
import com.hsd.app.model.HsdEffectinfo;
import com.hsd.app.service.IDataManageService;
import com.hsd.commons.model.HsdAccount;
import com.hsd.core.page.entity.PageParams;
import com.hsd.core.page.entity.Pagination;
import com.hsd.core.utils.TimeUtil;


@Controller
@RequestMapping("/datamanage")
public class EffectManageAction extends PLBaseAction{
	@Autowired
	private IDataManageService hpdataService;
	
	/**
	 * 数据管理 ：加载食药材信息列表
	 * @param pageParams
	 * @param request
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "/loadEffectList.do")
	public String loadEffectList(PageParams pageParams,HttpServletRequest request,ModelMap model){
		String effectname = request.getParameter("effectname"); 
		Pagination page = hpdataService.loadEffectList(pageParams,effectname);
		
		model.addAttribute("page",page);
		model.addAttribute("effectname",effectname); 
		return "app/data/data-effect-list";
	} 
	/**
	 * 新增一个食药材
	 * @param request
	 * @param model
	 * @return
	 */
	@RequestMapping(value="/addOneEffect.do",produces = {"application/json;charset=UTF-8"})
	public @ResponseBody String addOneEffect(HsdEffectinfo effectobj,HttpServletRequest request,ModelMap model){
		Map query = new HashMap();
		query.put("effectname", effectobj.getEffectname());
		query.put("youxiaobj", new Long(1));
		Object existData = hpdataService.getUniqueObject(HsdEffectinfo.class, query);
		if(null != existData){
			return "该功效已存在！";
		}
		effectobj.setId(this.hpdataService.getSeqNextVal("seq_hsd_effectinfo_id"));
		System.out.println("effectobj:" + effectobj.getType());
		HsdAccount accInfo = this.getCurrentuser(request); 
		effectobj.setYouxiaobj(PLCONSTANTS.YOUXIAOBJ_YX);
		effectobj.setOpertime(TimeUtil.getCurrentDate());
		if(null != accInfo){
			effectobj.setUserid(accInfo.getId());
			effectobj.setUsername(accInfo.getUsername());
		}
		hpdataService.save(effectobj);  
		return "保存成功！";
	}
	/**
	 * 查询一个功效
	 * @param request
	 * @param model
	 * @return
	 */
	@RequestMapping(value="/selectOneEffect.do",produces = {"application/json;charset=UTF-8"})
	public String selectOneEffect(HttpServletRequest request,ModelMap model){ 
		 String id = request.getParameter("id");
		 HsdEffectinfo effectinfo = (HsdEffectinfo) this.hpdataService.findByID(HsdEffectinfo.class, new Long(id));
		 model.put("effectinfo", effectinfo);
		 return "/app/data/insert-one-effect";
	}
	/**
	 * 修改一个食药材
	 * @param request
	 * @param model
	 * @return
	 */
	@RequestMapping(value="/editOneEffect.do",produces = {"application/json;charset=UTF-8"})
	public @ResponseBody String editOneEffect(HsdEffectinfo effectinfo,HttpServletRequest request,ModelMap model){ 
		String id = request.getParameter("id"); 
		HsdEffectinfo updateEffectinfo = (HsdEffectinfo) this.hpdataService.findByID(HsdEffectinfo.class, new Long(id)); 
		updateEffectinfo.setEffectname(effectinfo.getEffectname());
		updateEffectinfo.setType(effectinfo.getType());
		updateEffectinfo.setOpertime(TimeUtil.getCurrentDate());
		updateEffectinfo.setMemo(effectinfo.getMemo());
		hpdataService.saveObject(updateEffectinfo);
		return "修改成功！";
	}
	
	
	/**
	 * 删除食药材
	 * @param request
	 * @param model
	 * @return
	 */
	@RequestMapping(value="/delOneEffect.do",produces = {"application/json;charset=UTF-8"})
	public @ResponseBody String delOneEffect(HttpServletRequest request,ModelMap model){
		String id = request.getParameter("id"); 
		Map paramMap = new HashMap();
		paramMap.put("effectid", new Long(id));
		List datalist  = this.hpdataService.findList(HsdEffctRelation.class, paramMap);
		if(datalist != null && datalist.size() >0){
			return "该功效已经使用，不能删除！";
		}
		HsdEffectinfo effectObj = (HsdEffectinfo)hpdataService.findByID(HsdEffectinfo.class, new Long(id));
		effectObj.setYouxiaobj(PLCONSTANTS.YOUXIAOBJ_WX);
		hpdataService.saveObject(effectObj);
		return "删除成功！";
	}
}
